<?php


namespace app\common\model;

use app\common\model\Common as CommonModel;

class Region extends CommonModel
{
    protected $pk = 'id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 10;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);


        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        $re['page'] = $page;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];
//        if(isset($post['parentid'])){
//            $where[] = ['parentid','=',$post['parentid']];
//        }
        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = ['sort'=>'asc',$pk => 'asc'];
        return $result;
    }

    protected function tableFormat($list)
    {

        return $list;
    }


    public function handle($param){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        $act = $param['act'];

        $data['siteid'] = ADMIN_SITEID;

        $data['parentid'] = isset($param['parentid'])?$param['parentid']:0;
        if($data['parentid']){
            $m_catdir =  $this -> where('id',$data['parentid'])->find();
            $data['arrparentid'] = $m_catdir['arrparentid'].','.$data['parentid'];
        }else{
            $data['arrparentid'] = 0;
        }
        $data['catname'] = isset($param['catname'])?$param['catname']:'';

        if(!empty($param['picurl'])){
            $data['picurl'] = implode(',',$param['picurl']);
        }else{
            $data['picurl'] = '';
        }

        $data['keywords'] = isset($param['keywords'])?$param['keywords']:'';
        $data['is_open'] = isset($param['is_open'])?$param['is_open']:1;
        $data['orderid'] = isset($param['orderid'])?$param['orderid']:1;
        $data['moduleid'] = isset($param['moduleid'])?$param['moduleid']:79;
        $data['module'] = isset($param['module'])?$param['module']:'product';

        if($act == 'add'){
            $data['add_time'] = time();
            $r = $this->insertGetId($data);
            $id = $r;
            $ty = '添加';
        }
        if($act == 'edit'){
            $data['update_time'] = time();
            $r = $this->where('id','=',$param['id'])->update($data);
            $id = $param['id'];
            $ty = '修改';
        }
        if(!$r){
            $result['msg'] = '操作失败';
            return $result;
        }
        adminLog($data['catname'].'栏目'.$ty,$id);
        $result['code'] = 1;
        $result['msg'] = '操作成功';
        return $result;
    }

}